import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()]
    }),
    Components({
      resolvers: [ElementPlusResolver()]
    })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  server: {
    proxy: {
      '/api_region_search': {
        target: 'https://api.map.baidu.com/api_region_search', // 后端 API 的地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api_region_search/, '') // 如果后端 API 的路径中有前缀，需要进行重写
      },
      '/oauth': {
        target: 'https://aip.baidubce.com/oauth', // 后端 API 的地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/oauth/, '') // 如果后端 API 的路径中有前缀，需要进行重写
      },
      '/rpc': {
        target: 'https://aip.baidubce.com/rpc', // 后端 API 的地址
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/rpc/, '') // 如果后端 API 的路径中有前缀，需要进行重写
      }
    }
  }
})
